package softswitch.service;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import softswitch.model.OfferGroup;


@Stateless
public class OfferGroupDAO {

	
	@PersistenceContext(unitName = "persistence")
    private EntityManager em;
	
	
	public OfferGroup getById(Integer id){	
		return  em.find(OfferGroup.class, id);
	}

	public OfferGroup save(OfferGroup offergroup) {
    	em.persist(offergroup); 
    	return offergroup;
    }
	
	public OfferGroup update(OfferGroup offergroup) {
    	return em.merge(offergroup); 
    }
    
    public void delete(Integer id) {
    	em.remove(em.find(OfferGroup.class, id));	
    }
    
	@SuppressWarnings("unchecked")
    public List<OfferGroup> list(int first, int max) {    	
		Query query = em.createQuery("SELECT e FROM OfferGroup e");
    	if (max > 0) {
            query.setMaxResults(max);
        }
        if (first > 0) {
            query.setFirstResult(first);
        }
        return query.getResultList();   
    }
    
}

